import { AbstractControl, ValidationErrors } from "@angular/forms";

export class MyValidators {
    static cannotContainSpace (
        control: AbstractControl
    ): ValidationErrors | null {
        if (/\s/.test(control.value)) {
            // 验证失败返回对象，键一般与方法名相同
            return {
                cannotContainSpace: true
            }
        }
        // 验证成功返回 null
        return null
    }

    static shouldBeUnique (
        control: AbstractControl
    ): Promise<ValidationErrors | null> {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                if (control.value === 'admin') {
                    resolve({ shouldBeUnique: true })
                } else {
                    resolve(null)
                }
            }, 2000);
        })
    }
}